package com.gadberry.utility.expression.function;

import java.util.List;

import com.gadberry.utility.algorithm.Algorithm;
import com.gadberry.utility.algorithm.InvalidVariableUseException;
import com.gadberry.utility.expression.Argument;
import com.gadberry.utility.expression.Expression;
import com.gadberry.utility.expression.Function;
import com.gadberry.utility.expression.InvalidArgumentsException;

/**
 * Attempts to convert the argument to an integer.
 * 
 * @author Peter Lager
 *
 */
public class IntFunction extends Function {

	public IntFunction(Expression expression) {
		super(expression);
	}

	@Override
	public Argument resolve(Algorithm algor)
			throws InvalidVariableUseException {
		return new Argument(getArgument(0).toInteger(), getResolver());
	}

	@Override
	protected void checkArgs(List<Argument> args)
			throws InvalidArgumentsException {
		if (args.size() != 1) {
			throw new InvalidArgumentsException(
					"int requires a single argument.  Wrong number of arguments provided.");
		}

		for (Argument arg : args) {
			if(arg.isNull()) {
				throw new InvalidArgumentsException(
						"int cannot accept null arguments.  The argument provided is null.");
			} 
		}
		
	}

}
